<script setup >
import { ref, reactive, onUnmounted, computed } from "vue";
import {
  NBreadcrumb,
  NBreadcrumbItem,
  NSkeleton,
  NModal,
  NCard,
} from "naive-ui";
import { findOrderDetail } from "@/api/order";
import { usePayTime } from "@/utils";
import { useRoute } from "vue-router";

const route = useRoute();
const order = ref(null);
const { start, timeText } = usePayTime();

findOrderDetail(route.query.orderId).then((data) => {
  order.value = data;
  if (data.countdown > -1) {
    start(data.countdown);
  }
});

const redirect = encodeURIComponent("http://localhost:3000/pay/callback");
const payUrl = `http://pcapi-xiaotuxian-front-devtest.itheima.net/pay/aliPay?orderId=${route.query.orderId}&redirect=${redirect}`;

let showModal = ref(false);
</script>

<template>
  <div class="pay-page bg">
    <div class="container">
      <n-breadcrumb style="padding: 20px 0px">
        <n-breadcrumbItem href="/">首页</n-breadcrumbItem>
        <n-breadcrumbItem href="/cart">购物车</n-breadcrumbItem>
        <n-breadcrumbItem>支付订单</n-breadcrumbItem>
      </n-breadcrumb>
      <!-- 付款信息 -->
      <n-skeleton v-if="!order" height="240px" width="1240px"></n-skeleton>
      <div class="pay-info" v-else>
        <span class="icon iconfont icon-check-circle-fill"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>

          <p v-if="order.countdown > -1">
            支付还剩 <span>{{ timeText }}</span
            >, 超时后将取消订单
          </p>
          <p v-else>订单已经超时</p>
        </div>

        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ order.payMoney.toFixed(2) }}</span>
        </div>
      </div>

      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>

          <a
            target="_blank"
            @click="showModal = true"
            :href="payUrl"
            class="btn alipay"
          ></a>
          <a class="btn wx disabled" href="javascript:;"></a>

          <!-- 沙箱账号 -->
        </div>
        <h4 class="desc">
          <b>当前平台使用沙箱环境模拟支付!!!!</b> <br />
          <b><span>沙箱账号: deftkf9987@sandbox.com </span> </b><br />
          <b>
            <span>沙箱密码: 111111 </span>
          </b>
          <br />
          <b>
            <span>支付密码: 111111 </span>
          </b>
        </h4>
        <!-- <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div> -->
        <n-modal
          v-model:show="showModal"
          :mask-closable="false"
          :close-on-esc="false"
          style="width: 600px"
          preset="card"
        >
          <template #header>
            <div>正在支付...</div>
          </template>
          <n-card
            title=""
            :bordered="false"
            size="huge"
            role="dialog"
            aria-modal="true"
          >
            <div class="pay-wait">
              <img
                src="@/assets/images/d6113dba-fc75-462a-943f-789fd2fce7a4.png"
                alt=""
              />
              <div v-if="order">
                <p>如果支付成功：</p>
                <router-link :to="`/member/order/${route.query.orderId}`"
                  >查看订单详情></router-link
                >
                <p>如果支付失败：</p>
                <router-link to="/">查看相关疑问></router-link>
              </div>
            </div>
          </n-card>
        </n-modal>
      </div>
    </div>
  </div>
</template>

<style scoped lang='less'>
.disabled {
  opacity: 0.7;
  cursor: not-allowed;
}
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  .icon {
    font-size: 80px;
    color: #1dc779;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }
      &:last-child {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;
  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;
    &.active,
    &:hover {
      border-color: @xtxColor;
    }
    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }
    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}

.pay-wait {
  display: flex;
  justify-content: space-around;
  img {
    height: 200px;
    width: 200px;
  }
  p {
    margin-top: 30px;
    font-size: 14px;
  }
  a {
    color: @xtxColor;
  }
}
.desc {
  font-size: 15px;
  color: @priceColor;
  margin-top: 20px;
  margin-left: 30px;
  span {
    color: #333;
  }
}
</style>